﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RepoInfrastructure;

namespace ConferenceRegister.Domain
{
    public class Context : IUnitOfWork
    {
        ConferenceEntities context = new ConferenceEntities();

        private Repository<Conference> _Conferences;
        public Repository<Conference> Conferences
        {
            get { return _Conferences = _Conferences ?? new Repository<Conference>(context); }
            set { _Conferences = value; }
        }
        private Repository<Register> _Registrants;
        public Repository<Register> Registrants
        {
            get { return _Registrants = _Registrants ?? new Repository<Register>(context); }
            set { _Registrants = value; }
        }
        private Repository<Seat> _Seats;
        public Repository<Seat> Seats
        {
            get { return _Seats = _Seats ?? new Repository<Seat>(context); }
            set { _Seats = value; }
        }
        private ReadonlyRepository<vSeat> _vSeats;
        public ReadonlyRepository<vSeat> vSeats
        {
            get { return _vSeats = _vSeats ?? new ReadonlyRepository<vSeat>(context); }
            set { _vSeats = value; }
        }
        private Repository<Ministry> _Ministries;
        public Repository<Ministry> Ministries
        {
            get { return _Ministries = _Ministries ?? new Repository<Ministry>(context); }
            set { _Ministries = value; } 
        }
        private ReadonlyRepository<vMinistry> _vMinistries;
        public ReadonlyRepository<vMinistry> vMinistries
        {
            get { return _vMinistries = _vMinistries ?? new ReadonlyRepository<vMinistry>(context); }
            set { _vMinistries = value; }
        }
        private Repository<MinistrySignup> _MinistrySignups;
        public Repository<MinistrySignup> MinistrySignups
        {
            get { return _MinistrySignups = _MinistrySignups ?? new Repository<MinistrySignup>(context); }
            set { _MinistrySignups = value; }
        }

        public int SaveChanges()
        {
            return context.SaveChanges();
        }

        public void Dispose()
        {
            //SaveChanges();
            var list = new List<IDisposable> { _Conferences, _Registrants, _Ministries, _Seats, _vMinistries, _vSeats, _MinistrySignups };
            list.ForEach(e => { if (e == null) e.Dispose(); });
        }
    }
}
